package huv.andlogin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class andlogin extends Activity {
    /** Called when the activity is first created. */
    CfgParser cfgp; 
    int _conf_select;
    Dialog dialog=null;
    int state;
    int a2;
    ProgressDialog pd;
    WifiControl wf;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			cfgp = new CfgParser();
			wf = new WifiControl(this);
			this.registerReceiver(wf, new IntentFilter(
					WifiManager.NETWORK_STATE_CHANGED_ACTION)); 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		setContentView(R.layout.main);
		populateListView();
        
    }
	public void populateListView(){
		ListView l =(ListView)findViewById(R.id.e_list);
		List<HashMap<String,String>> ll = cfgp.listCfg();
		String[] from = {"name"};
		int[] to = { android.R.id.text1 };
		SimpleAdapter adp = new SimpleAdapter(this,ll,android.R.layout.simple_list_item_1,from,to);
		l.setAdapter(adp);
		l.setOnItemClickListener(new OnItemClickListener() {
										@Override
										public void onItemClick(
												AdapterView<?> arg0, View arg1,
												int arg2, long arg3) {
											// TODO Auto-generated method stub
											a2 = arg2;
											Cfg c = cfgp.get(cfgp.keySet().toArray()[a2]);
											Toast toast2 = Toast.makeText(getApplicationContext(), "Connecting...",Toast.LENGTH_SHORT);
											toast2.show();
											state = AutoLogin.login(c);
											String msg="";
											switch(state){
												case AutoLogin._CONNECTION_COMPLETE: msg = "Connection Complete"; break;
												case AutoLogin._INVALID_URL: msg = "Invalid URL"; break;
												case AutoLogin._IO_ERROR: msg = "IO Error"; break;
												case AutoLogin._KEY_MANAGMENT_ERROR: msg = "Key Management Error"; break;
												case AutoLogin._NO_SUCH_ALGORITHM: msg = "No such Algorithm"; break;
												case AutoLogin._PROTOCOL_ERROR: msg = "Protocol Error"; break;
												case AutoLogin._CONNECTION_CLOSE_ERROR: msg = "Protocol Error"; break;
												case AutoLogin._DATA_OUTPUT_STREAM_INIT_ERROR: msg = "Data Output Stream Error"; break;
												case AutoLogin._WRITE_BYTE_ERROR: msg = "Write Bytes Error"; break;
												case AutoLogin._UNABLE_OPEN_CONNECTION: msg = "Unable to open Connection"; break;
												default: msg= "oh cazzo"; break;
											}
											Toast toast = Toast.makeText(getApplicationContext(), msg,Toast.LENGTH_SHORT);
											toast.show();
											
											
										} });
		l.setOnItemLongClickListener(new OnItemLongClickListener() {
											@Override
											public boolean onItemLongClick(
													AdapterView<?> arg0, View arg1,
													int arg2, long arg3) {
												_conf_select = arg2;
												showDialog(1);
												//TODO Auto-generated method stub
												return true;
			} });
		
		
		
	
	}
	protected Dialog onCreateDialog(int id) {
	   
	    switch(id) {
	    case 1:
	       	dialog = new Dialog(this);
			dialog.setContentView(R.layout.edit_dialog);
			dialog.setTitle("Set Post Parameter");
			LinearLayout v_dialog= (LinearLayout) dialog.findViewById(R.id.Parameter);
			Cfg c = cfgp.get(cfgp.keySet().toArray()[_conf_select]);
			@SuppressWarnings("unchecked")
			Iterator<Entry<String,String>> i =(Iterator<Entry<String,String>>) c.getPRIterator();
			int jd = 2000;
			while(i.hasNext()){
				LinearLayout tl = new LinearLayout(dialog.getContext());
				TextView tt = new TextView(dialog.getContext());
				Entry<String,String> e = i.next();
				tt.setText(e.getKey());
				EditText te = new EditText(dialog.getContext());
				te.setWidth(200);
				te.setId(jd);
				jd++;
				te.setText(e.getValue());
				tl.addView(tt);
				tl.addView(te);
				v_dialog.addView(tl);
			}
	    	Button b = (Button)dialog.findViewById(R.id.Save);
	    	b.setOnClickListener(new OnClickListener(){
										@Override
											public void onClick(View v) {
											//TODO Auto-generated method stub
												Cfg c = cfgp.get(cfgp.keySet().toArray()[_conf_select]);
												@SuppressWarnings("unchecked")
												Iterator<Entry<String,String>> i =(Iterator<Entry<String,String>>) c.getPRIterator();
												int id = 2000;
												while(i.hasNext()){
													EditText te = (EditText) dialog.findViewById(id); 
													Entry<String,String> e = i.next();
													e.setValue(te.getText().toString());
													id++;
												}
												cfgp.saveState();
												dialog.dismiss();
											}
										});
	        break;
	    
	    default:
	        dialog = null;
	    }
	    return dialog;
	}
	
}